package flex.cc.vehicle_svr.controller;

import java.net.URLDecoder;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.ParserConfig;

import flex.cc.common.utils.DateUtil;
import flex.cc.core.support.BaseController;
import flex.cc.order.manage.service.OrderImgService;
import flex.cc.vehicle_svr.model.UpLoadImageModel;
import flex.cc.vehicle_svr.vo.ResponseVo;

/**
 * 下游照片同步接口
 */
@RestController
@RequestMapping(value = "/vehicleSvr")
public class SynImgController extends BaseController {

	private ParserConfig parserConfig = new ParserConfig ();
	
	@Resource
	private OrderImgService orderImgService;
	
	/**
	 * 
	 * @author WANGCONG
	 * 2021年9月7日上午9:36:48
	 * @desc 下游照片同步
	 */
	@ResponseBody
    @RequestMapping("/synImg")
    public JSONObject synImg(HttpServletRequest request) throws Exception {
    	// 进入接口时间
    	long startTime = System.currentTimeMillis();
    	logger.info("vehicleSvr/synImg 进入接口:time=" + DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
    	//生成响应报文
        JSONObject jsonResult = new JSONObject();
        //读取报文
        try{
        	//设置编码
            request.setCharacterEncoding("UTF-8");
            // 读取请求报文 
	        String requestJson = IOUtils.toString(request.getInputStream(), "UTF-8");
	        requestJson = URLDecoder.decode(requestJson, "UTF-8");
	        logger.info("vehicleSvr/synImg requestJson={}", requestJson);
	        if(requestJson != null && !"".equals(requestJson)) {
	        	// 调用业务处理 =============================
	        	UpLoadImageModel imgVo = JSON.parseObject (requestJson, UpLoadImageModel.class, parserConfig, JSON.DEFAULT_PARSER_FEATURE);
	        	if ( ObjectUtils.isEmpty ( imgVo ) || ObjectUtils.isEmpty ( imgVo.getOrderNo()) ) { //报文为空或者救援单号为空
	        		jsonResult.put("resultCode", ResponseVo.ERROR_CODE);
	        		jsonResult.put("message", "报文格式不正确");
	        		// return jsonResult;
	        	}else{
	        		Map<String, String> resultMap = orderImgService.synImg(imgVo);
	        		String code = resultMap.get("code");
	        		if("1".equals(code)){
	        			jsonResult.put("resultCode", ResponseVo.ERROR_CODE);
		        		jsonResult.put("message", resultMap.get("msg"));
		        		// return jsonResult;
	        		} else {
	        			jsonResult.put("resultCode", ResponseVo.SUCCESS_CODE);
	        			jsonResult.put("message", ResponseVo.SUCCESS_MSG);
	        		}
	        	}
	        } else {
	        	logger.error("vehicleSvr/synImg 接口处理异常:请求报文为空");
	        	jsonResult.put("resultCode", ResponseVo.ERROR_CODE);
        		jsonResult.put("message", "报文为空或者救援单号为空");
        		// return jsonResult;
	        }
        }catch(Exception e){
        	// e.printStackTrace();
        	logger.error("vehicleSvr/synImg 数据接收异常", e);
        	jsonResult.put("resultCode", ResponseVo.ERROR_CODE);
    		jsonResult.put("message", "数据接收异常");
    		// return jsonResult;
        }
        // 离开接口时间
        long endTime = System.currentTimeMillis();
        logger.info("vehicleSvr/synImg 离开接口:responseJson={}, time={}; 处理时长:{}", JSON.toJSONString(jsonResult), DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"), (endTime - startTime));
		return jsonResult;
    }
}
